using System;
using System.Text;
using System.Windows.Forms;

namespace EIM_Accounting
{
    /// <summary>
    /// 录入和计算 月考勤 『员工考勤录入』
    /// </summary>
    public partial class EC010102 : Form
    {
        BLL.EC010102 ec010102 = new BLL.EC010102();

        public EC010102()
        {
            InitializeComponent();
        }

        private void input_float_Press(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
            {
                e.Handled = true;
            }

            if (e.KeyChar == 46)                       //小数点
            {
                string txtValue = ((System.Windows.Forms.TextBox)sender).Text;

                if (txtValue.Length <= 0)
                    e.Handled = true;           //小数点不能在第一位
                else
                {
                    float f;
                    float oldf;
                    bool b1 = false, b2 = false;
                    b1 = float.TryParse(txtValue, out oldf);
                    b2 = float.TryParse(txtValue + e.KeyChar.ToString(), out f);
                    if (b2 == true)
                    {
                        if (b1 == false)
                            e.Handled = true;
                        else
                            e.Handled = false;
                    }
                }
            }
        }

        private void EC010102_Load(object sender, System.EventArgs e)
        {
            cmbStaff.DataSource = ec010102.getMemberList().Tables[0];
            cmbStaff.DisplayMember = "Name";
            cmbStaff.ValueMember = "ID";

            dtpMonth.MaxDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1);
            dtpMonth.Value = DateTime.Now.AddMonths(-1);

            txtLegalDays.Text = ec010102.getLegalDays(dtpMonth.Value, cmbStaff.SelectedValue.ToString());
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (double.Parse(txtFactDays.Text) > double.Parse(this.txtLegalDays.Text))
                {
                    txtFactDays.Focus();
                    throw new Exception();
                }
                if (double.Parse(txtLegalDays.Text) > 24)
                {
                    MessageBox.Show("请检查法定工作日是否正确？");
                    return;
                }
                Accounting.Model.WagesDetail model = new Accounting.Model.WagesDetail();
                Accounting.Model.WagesProvide modelProvide = new Accounting.Model.WagesProvide();

                model.StaffID = int.Parse(this.cmbStaff.SelectedValue.ToString());
                model.AttendanceMonth = this.dtpMonth.Value.ToString("yyyyMM");
                model.FactDays = decimal.Parse(this.txtFactDays.Text);
                model.LegalDays = decimal.Parse(txtLegalDays.Text);
                model.BaseMoney = decimal.Parse(this.txtBaseMoney.Text);

                if (this.txtAddWorks.Text.Trim() != string.Empty)
                    model.AddWork = decimal.Parse(txtAddWorks.Text);
                if (this.txtAddWorkBase.Text.Trim() != string.Empty)
                    model.AddWorkBaseMoney = decimal.Parse(txtAddWorkBase.Text);
                if (this.txtAward.Text.Trim() != string.Empty)
                    model.Award = decimal.Parse(txtAward.Text);
                if (this.txtP.Text.Trim() != string.Empty)
                    model.Punishment = decimal.Parse(txtP.Text);

                model.Remark = txtRemark.Text;

                //---------------------modelProvide
                if (this.txtAddWorks.Text.Trim() != string.Empty && txtAddWorkBase.Text.Trim() != string.Empty)
                    modelProvide.AddWork = decimal.Parse(txtAddWorks.Text) * decimal.Parse(txtAddWorkBase.Text);
                if (this.txtAward.Text.Trim() != string.Empty)
                    modelProvide.Award = decimal.Parse(txtAward.Text);
                if (this.txtP.Text.Trim() != string.Empty)
                    modelProvide.Punishment = 0 - Math.Abs(decimal.Parse(txtP.Text));

                modelProvide.StaffID = int.Parse(this.cmbStaff.SelectedValue.ToString());
                modelProvide.ProvideMonth = this.dtpMonth.Value.ToString("yyyyMM");
                modelProvide.BaseWage = decimal.Parse(this.txtFactDays.Text) * decimal.Parse(this.txtBaseMoney.Text)
                    / decimal.Parse(txtLegalDays.Text);

                modelProvide.ProvideFee = modelProvide.BaseWage;
                if (modelProvide.AddWork != null)
                    modelProvide.ProvideFee += modelProvide.AddWork;
                if (modelProvide.Award != null)
                    modelProvide.ProvideFee += modelProvide.Award;
                if (modelProvide.Punishment != null)
                    modelProvide.ProvideFee += modelProvide.Punishment;
                //-------------modelProvide End

                Accounting.Model.WagesDetail oldmodel = ec010102.getModelByMonthStaff(int.Parse(this.cmbStaff.SelectedValue.ToString()), model.AttendanceMonth);

                if (oldmodel == null)
                {
                    ec010102.addModel(model, modelProvide);
                }
                else
                {
                    Accounting.Model.WagesProvide oldModelProvide = ec010102.getProvideModelByMonthStaff(int.Parse(this.cmbStaff.SelectedValue.ToString()), model.AttendanceMonth);

                    if (oldModelProvide.ProvideDate != null)
                    {
                        MessageBox.Show("已发放，不能再修改！");
                        return;
                    }
                    model.ID = oldmodel.ID;
                    modelProvide.ID = oldModelProvide.ID;
                    StringBuilder msgStr = new StringBuilder();
                    msgStr.Append("数据库中已经存在数据，是否替换？\n");
                    msgStr.Append("--------------------------------------------------------------------------------\n");
                    msgStr.Append("实际工作日：").Append(oldmodel.FactDays).Append("\n");
                    msgStr.Append("加班：").Append(oldmodel.AddWork).Append("\n");
                    if (MessageBox.Show(msgStr.ToString(), "", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
                    {
                        ec010102.updateModel(model, modelProvide);
                    }
                }
                this.Close();
            }
            catch
            {
                MessageBox.Show("数据不正确！");
            }
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}